#include <stdio.h>
#include <time.h>
#include "test.h"
#include "login.h"
#include "cal.h"
#include "cal.c"

int main () {
    // if ( !login() ) {
    //     printf("Log in err\n");
    //     exit(-9);
    // }
    
    int choice, i = 0, j = 0, key, ans, abcd[4], correct = 0, incorrect = 0, total_correct, total_incorrect, offline, option = 0, err = 0;
    int time_start, time_end, time_cost, total_time, m, s;
    int state = 0, id;
    int length, wrong_keyi, wrong_ans;
    char cho, expression[13] = {0}, errexpression[4096] = {0}, tmp_wrong[16384] = {0}, wrong_keyc[2] = {0}, wrong_ansc;
    FILE* offlinetest, * offlinekey, * help, * wrong, * record, * person;
    srand(time(NULL));
    while (1) {
        printf("Please input your choice\n\
1  10 questions\t2  Records\t3  Wrongs\n\
4  Offline\t5  Help\t\t6  Exit\nYour choice is\t");
        scanf("%d",&choice);
        switch (choice) {
        case 1:
            person = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/person.txt","r");
            fscanf(person,"%d,%d,%d",&total_correct,&total_incorrect,&total_time);
            fclose(person);
            printf("I Solemnly Swear That This Set Of Examination Papers Was Completed By Myself.\nInput 1 to Agree, input other to Disagree.\n");
            scanf("%d",&state);
            if ( state != 1 ) exit(-2);
            wrong = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/wrong.txt","a");
            time_start = time(NULL);
            for ( i = 0; i < 10; i++ ) {
                printf("\nTest %d\n",i+1);
                record = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/record.txt","w");
                key = cal(1,record,offlinetest);
                fclose(record);
                switch ( rand() % 2 )
                {
                case 0:
                    printf("Please input the answer: ");
                    scanf("%d",&ans);
                    if ( ans == key ) {
                        correct++;
                        printf("Correct\n");
                    } else {
                        incorrect++;
                        printf("Incorrect\n");
                        record = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/record.txt","r");
                        fgets(expression,13,record);
                        fprintf(wrong,"-\n%s\n",expression);
                        // fprintf(wrong,"-\n%s\n%d\n",expression,ans);
                        fclose(record);
                    }
                    break;
                case 1:
                    state = 0;
                    while (1) {
                        for ( j = 0; j < 4; j++ ) {
                            abcd[j] = rand() % 100;
                            if ( abcd[j] == 0 ) j--;
                        }
                        if ( abcd[0] == abcd[1] || abcd[0] == abcd[2] || abcd[0] == abcd[3] \
                        || abcd[1] == abcd[2] || abcd[1] == abcd[3] || abcd[2] == abcd[3] ) continue;
                        break;
                    }
                    for ( j = 0; j < 4; j++ ) {
                        if ( abcd[j] == key ) {
                            state = 1;
                            break;
                        }
                    }
                    if ( state != 1 ) {
                        j = rand() % 4;
                        abcd[j] = key;
                    }
                    printf("A %2d\tB %2d\nC %2d\tD %2d\n",abcd[0], abcd[1], abcd[2], abcd[3]);
                    record = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/record.txt","a");
                    fprintf(record,"A %2d\tB %2d\nC %2d\tD %2d\n",abcd[0], abcd[1], abcd[2], abcd[3]);
                    fclose(record);
                    printf("Please input your choice: ");
                    scanf("%s", &cho);
                    cho-=65;
                    if ( cho == j ) {
                        correct++;
                        printf("Correct\n");
                    } else {
                        incorrect++;
                        printf("Incorrect\n");
                        record = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/record.txt","a+");
                        rewind(record);
                        fgets(expression,10,record);
                        fclose(record);
                        fprintf(wrong,"+\n%s\n",expression);
                        fprintf(wrong,"A %2d\tB %2d\nC %2d\tD %2d\n%c\n",abcd[0], abcd[1], abcd[2], abcd[3],j+65);
                        // fprintf(wrong,"%c\n",cho+65);
                    }
                    break;
                }
            }
            time_end = time(NULL);
            time_cost = time_end - time_start;
            printf("Well Done\nCorrect %d\nIncorrect %d\nYou used %ds to answer these questions\n",correct,incorrect,time_cost);
            total_correct+=correct;
            total_incorrect+=incorrect;
            total_time+=time_cost;
            person = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/person.txt","w");
            fprintf(person,"%d,%d,%d",total_correct,total_incorrect,total_time);
            fclose(person);
            fclose(wrong);
            break;

        case 2:
            person = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/person.txt","r");
            fscanf(person,"%d,%d,%d",&total_correct,&total_incorrect,&total_time);
            fclose(person);
            if ( total_time <= 60 )
            printf("Dear user, You have answered %d questions\n\
Total correct %d, Total incorrect %d, Total time %ds\n\
input 0 to clear memory, input other to exit to menu\n\
",total_correct+total_incorrect,total_correct,total_incorrect,total_time);
            else {
                m = total_time / 60;
                s = total_time % 60;
                printf("Dear user, You have answered %d questions\n\
Total correct %d, Total incorrect %d, Total time %dmin%ds\n\
input 0 to clear memory, input other to exit to menu\n\
",total_correct+total_incorrect,total_correct,total_incorrect,m,s);
            }
            
            scanf("%d",&ans);
            if ( !ans ) {
                printf("Are you sure?\nInput 1 to continue, input other to exit\n");
                scanf("%d",&state);
                if ( state == 1 ) {
                    person = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/person.txt","w");
                    fprintf(person,"0,0");
                    fclose(person);
                    printf("Clear success!\n");
                }
            };
            break;

        case 3:
            i = 0, state = 0; /* state == 0 ~ No incorrect */
            wrong = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/wrong.txt","r");
            if ( !wrong ) {
                printf("Err while opening wrong\n");
                exit(-3);
            }
            if ( feof(wrong) ) printf("Good! There is no wrong in the record.\nTry to do some test!\n");
            while (!feof(wrong)) {
                // tmp_wrong[i] = fgetc(wrong);
                // i++;
                tmp_wrong[i] = fgetc(wrong);
                i++;
                tmp_wrong[i] = fgetc(wrong);
                i++;
                if ( tmp_wrong[i-2] == '-' ) {
                    /* Fill in the Blanks */
                    for ( j = 0; j < 10; j++ ) {
                        tmp_wrong[i] = fgetc(wrong);
                        printf("%c",tmp_wrong[i]);
                        i++;
                    }
                    tmp_wrong[i] = fgetc(wrong);
                    wrong_keyc[0] = tmp_wrong[i];
                    i++;
                    tmp_wrong[i] = fgetc(wrong);
                    wrong_keyc[1] = tmp_wrong[i];
                    i++;
                    tmp_wrong[i] = fgetc(wrong);
                    wrong_keyi = atoi(wrong_keyc);
                    scanf("%d",&wrong_ans);
                    if ( wrong_ans == wrong_keyi ) {
                        printf("Correct\n");
                        for ( j = 0; j <= 14; j++ ) {
                            tmp_wrong[i] = 0;
                            i--;
                        }
                        i++;
                    } else {
                        printf("Incorrect\n");
                        state = 1;
                        i++;
                    }
                } else if ( tmp_wrong[i-2] == '+' ) {
                    /* multiple choices */
                    for ( j = 0; j < 30; j++ ) {
                        tmp_wrong[i] = fgetc(wrong);
                        printf("%c",tmp_wrong[i]);
                        i++;
                    }
                    tmp_wrong[i] = fgetc(wrong);
                    i++;
                    tmp_wrong[i] = fgetc(wrong);
                    scanf("%s",&wrong_ansc);
                    if ( wrong_ansc == tmp_wrong[i-1] ) {
                        printf("Correct\n");
                        for ( j = 0; j <= 33; j++ ) {
                            tmp_wrong[i] = 0;
                            i--;
                        }
                        i++;
                    } else {
                        printf("Incorrect\n");
                        i++;
                        state = 1;
                    }
                }
            }
            if ( state == 0 ) printf("\nGood Job! No Incorrect!\nGo to the menu, input 1 to try something new!\n");
            tmp_wrong[i-2] = '\0';
            tmp_wrong[i-1] = 0;
            fclose(wrong);
            wrong = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/wrong.txt","w");
            fprintf(wrong,"%s",tmp_wrong);
            fclose(wrong);
            break;
        case 4:
            offlinetest = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/offlinetest.txt","w");
            offlinekey = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/offlinekey.txt","w");
            record = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/record.txt","r");
            while (1) {
                printf("Input how many questions you want to form\n");
                scanf("%d",&offline);
                if ( offline > 0 && offline < 501 ) break;
                printf("Don't do too much tests!\n");
            }
            id = (rand() % 100000)*offline;
            fprintf(offlinetest,"***  Offline test %x  ***\t\t\t%d questions\nSid________    Class________    Name________    Score________\n\
[ ]I Solemnly Swear That This Set Of Examination Papers Was Completed By Myself\n\n",id,offline);
            fprintf(offlinekey,"Key To Offline test %x\nCheck after you complete the test\n\n",id);
            printf("Input what kind of questions you want to form\n\
input 1 to form Filling the blanks, input 2 to form Multiple choices, input other to form ramdon\n");
            scanf("%d",&ans);
            printf("Forming %x test\n",id);
            if ( ans == 1 ) option = 0;
            else if ( ans == 2 ) option = 1;
            for ( i = 0; i < offline; i++ ) {
                fprintf(offlinetest,"%3d.  ",i+1);
                key = cal(2,record,offlinetest);
                if ( ans != 1 && ans != 2 ) option = ( rand() % 2 );
                switch ( option ) {
                case 0:
                    fprintf(offlinetest,"____\n\n");
                    fprintf(offlinekey,"%3d. %d\n",i+1,key);
                    printf("%d\tOffline Filling the Blanks has Formed Successly.\n",i+1);
                    break;
                case 1:
                    state = 0;
                    while (1) {
                        for ( j = 0; j < 4; j++ ) {
                            abcd[j] = rand() % 100;
                            if ( abcd[j] == 0 ) j--;
                        }
                        if ( abcd[0] == abcd[1] || abcd[0] == abcd[2] || abcd[0] == abcd[3] \
                        || abcd[1] == abcd[2] || abcd[1] == abcd[3] || abcd[2] == abcd[3] ) continue;
                        break;
                    }
                    for ( j = 0; j < 4; j++ ) {
                        if ( abcd[j] == key ) {
                            state = 1;
                            break;
                        }
                    }
                    if ( state != 1 ) {
                        j = rand() % 4;
                        abcd[j] = key;
                    }
                    fprintf(offlinetest,"(   )\n\tA %2d\tB %2d\tC %2d\tD %2d\n\n",abcd[0], abcd[1], abcd[2], abcd[3]);
                    j+=65;
                    fprintf(offlinekey,"%3d. %c\n",i+1,j);
                    printf("%d\tOffline Multiple Choices has Formed Successly.\n",i+1);
                    break;
                }
            }

            fclose(offlinetest);
            fclose(offlinekey);
            fclose(record);
            option = 0;
            break;

        case 5:
            help = fopen("/home/data/cppproj/0809ZJU007A/njupt/calculator/readme.txt","r");
            break;

        case 6:
            printf("Thank you\nEnjoy yourselves!\n");
            exit(0);

        default:
            printf("\nSorry\n");
            err++;
            if ( err > 5 ) {
                printf("Input Err Too Many Times!\n");
                exit(-1);
            }
            break;
        }
    }
    return 0;
}